<?php

namespace app\admin\controller;

use app\admin\model\jeek\shop\SkuGoods;
use app\common\controller\Backend;
use app\common\model\User;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 提现管理
 *
 * @icon fa fa-circle-o
 */
class Withdraw extends Backend
{

    /**
     * Withdraw模型对象
     * @var \app\common\model\Withdraw
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\Withdraw;
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("positionList", $this->model->getPositionList());
    }


    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with(['user', 'skuGoods','copartner'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }


    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if (false === $this->request->isPost()) {
            $this->view->assign('row', $row);
            if ($row->user_id != 0) {
                $row->nickname = User::where('id', $row->user_id)->value('nickname');
            }
            if ($row->goods_id != 0) {
                $row->name = SkuGoods::where('id', $row->goods_id)->value('name');
            }
            if ($row->copartner_id != 0) {
                $row->copartner_name = \app\common\model\Copartner::where('id', $row->copartner_id)->value('name');
            }
            return $this->view->fetch();
        }
        $params = $this->request->post('row/a');
        if (empty($params)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }
        if ($params['status'] == 2 && $row->status == 3) {
            # 你已拒绝申请提现！
            $this->error('已拒绝提现，请勿重复操作');
        }
        if ($params['status'] == 3 && $row->status == 2) {
            $this->error('您已申请提现，请勿拒绝操作');
        }
        if ($params['status'] == 2 && $row->status == 1) {
            # 同意提现 打款给司机

        }
        if ($params['status'] == 3 && $row->status == 1) {
            # 拒绝提现
            if (!$params['reason_rejection']) {
                $this->error('请填写拒绝原因');
            }
            if ($params['goods_id'] != 0) {
                SkuGoods::money($params['price'], $params['goods_id'], '提现拒绝');
            }
            # 合伙人拒绝提现
            if ($params['copartner_id'] != 0) {
                \app\common\model\Copartner::money($params['price'], $params['copartner_id'],0, '提现拒绝',$row->id);
            }
        }
        $result = false;
        Db::startTrans();
        try {
            //是否采用模型验证
            if ($this->modelValidate) {
                $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                $row->validateFailException()->validate($validate);
            }
            $result = $row->allowField(true)->save($params);
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if (false === $result) {
            $this->error(__('No rows were updated'));
        }
        $this->success();
    }


}
